package com.study.retrofitsample;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.study.retrofitrealize.MyRetrofit;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView tv_text;
    private API api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String baseUrl = "https://restapi.amap.com";
        MyRetrofit retrofit = new MyRetrofit.Builder().baseUrl(baseUrl).build();

        //创建接口访问类
        api = retrofit.create(API.class);

        tv_text = findViewById(R.id.tv_text);
        findViewById(R.id.btn_get).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
                tv_text.setText("");
            }
        });
        findViewById(R.id.btn_post).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postData();
                tv_text.setText("");
            }
        });
    }

    /**
     * 获取数据POST
     */
    private void postData() {
        api.postWeather("110101", "ae6c53e2186f33bbf240a12d80672d1b")
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        Log.e("Http------ERR", Objects.requireNonNull(e.getMessage()));
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                        final String res = Objects.requireNonNull(response.body()).string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tv_text.setText(res);
                            }
                        });
                    }
                });
    }

    /**
     * 获取数据GET
     */
    private void getData() {
        api.getWeather("110101", "ae6c53e2186f33bbf240a12d80672d1b")
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        Log.e("Http------ERR", Objects.requireNonNull(e.getMessage()));
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                        final String res = Objects.requireNonNull(response.body()).string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tv_text.setText(res);
                            }
                        });
                    }
                });
    }
}